package com.zkzx.driver.service;

import com.zkzx.common.driver.command.DriverCommand;
import com.zkzx.common.driver.model.DriverBankVO;
import com.zkzx.driver.mapper.DriverBankMapper;
import com.zkzx.driver.mapper.entity.DriverBank;
import com.zkzx.driver.mapper.entity.DriverBankExample;
import com.zkzx.util.data.HideDataUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/08/9:49
 * @Description:
 */
@Service
public class DriverBankService {

    @Resource
    private DriverBankMapper driverBankMapper;

    /**
     * 根据司机id查询银行卡信息
     * @param driverId 企业id
     * @return 银行卡信息
     */
    public DriverBankVO findVOByDriverId(Integer driverId){
        DriverBank driverBank = findByDriverId(driverId);
        if(Objects.isNull(driverBank)){
            return null;
        }
        return DriverBankVO.builder()
                .cardNumber(driverBank.getCardnumber())
                .name(driverBank.getName())
                .mobile(driverBank.getMobile())
                .build();
    }
    /**
     * 根据司机id查询银行卡信息
     * @param driverId 企业id
     * @return 银行卡信息,卡号隐藏
     */
    public DriverBankVO findVOByDriverIdHide(Integer driverId){
        DriverBank driverBank = findByDriverId(driverId);
        if(Objects.isNull(driverBank)){
            return null;
        }
        return DriverBankVO.builder()
                .cardNumber(HideDataUtil.getHideData(driverBank.getCardnumber()))
                .name(driverBank.getName())
                .build();
    }

    /**
     * 根据司机id查询银行卡信息
     * @param driverId 企业id
     * @return 银行卡信息
     */
    public DriverBank findByDriverId(Integer driverId){

        DriverBankExample driverBankExample = new DriverBankExample();
        driverBankExample.createCriteria().andDriveridEqualTo(driverId);
        Optional<DriverBank> driverBankOne = driverBankMapper.selectByExample(driverBankExample).stream().findFirst();

        DriverBank driverBank  = null;
        if(driverBankOne.isPresent()){
            driverBank = driverBankOne.get();
        }
        return driverBank;
    }

    /**
     * 更新银行卡信息
     * @param command 入参
     * @param id 司机id
     */
    public void update(DriverCommand command, Integer id){
        if(Objects.isNull(command.getDriverBank())){
            return;
        }
        DriverBank driverBank = new DriverBank();
        driverBank.setId(command.getDriverBank().getId());
        driverBank.setCardnumber(command.getDriverBank().getCardNumber());
        driverBank.setMobile(command.getDriverBank().getMobile());
        driverBank.setName(command.getDriverBank().getName());
        DriverBankExample driverBankExample = new DriverBankExample();
        DriverBankExample.Criteria criteriaBank = driverBankExample.createCriteria();
        criteriaBank.andDriveridEqualTo(id);
        driverBankMapper.updateByExampleSelective(driverBank,driverBankExample);

    }
    public void insertOrUpdateBank(DriverCommand command, Integer id){
        if (Objects.nonNull(command.getDriverBank())){
            DriverBank driverBank = new DriverBank();
            driverBank.setDriverid(id);
            driverBank.setCardnumber(command.getDriverBank().getCardNumber());
            driverBank.setMobile(command.getDriverBank().getMobile());
            driverBank.setName(command.getDriverBank().getName());

            DriverBank byDriverId = this.findByDriverId(id);

            if(Objects.isNull(byDriverId)){
                driverBankMapper.insert(driverBank);
                return;
            }

            DriverBankExample driverBankExample = new DriverBankExample();
            driverBankExample.createCriteria().andIdEqualTo(byDriverId.getId());
            driverBankMapper.updateByExampleSelective(driverBank,driverBankExample);

        }
    }
    /**
     * 插入银行卡信息
     * @param command 入参
     * @param id 司机id
     */
    public void insert(DriverCommand command, Integer id){
        if(Objects.isNull(command.getDriverBank())){
            return;
        }
        DriverBank driverBank = new DriverBank();
        driverBank.setDriverid(id);
        Optional.ofNullable(command.getDriverBank()).ifPresent(db->{
        driverBank.setCardnumber(command.getDriverBank().getCardNumber());
        driverBank.setMobile(command.getDriverBank().getMobile());
        driverBank.setName(command.getDriverBank().getName());
        });
        driverBankMapper.insert(driverBank);
    }
}
